//
//  ZJFtitleView.m
//  OurTourism
//
//  Created by 张金峰 on 15/11/24.
//  Copyright © 2015年 Devil_Coming. All rights reserved.
//
#define VWIDTH self.frame.size.width
#define VHEIGHT self.frame.size.height

#import "ZJFtitleView.h"

@interface ZJFtitleView ()<UIScrollViewDelegate>
@property (nonatomic, retain)UIButton *btn;
@property (nonatomic, retain)UILabel *pointView;
@end
@implementation ZJFtitleView

- (instancetype)initWithFrame:(CGRect)frame
                     titleArr:(NSMutableArray *)titleArr
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createView:titleArr];
    }
    return self;
}
- (void)createView:(NSMutableArray *)titleArr;
{
    
    
    self.titleArr = titleArr;
    self.scrollView = [[UIScrollView alloc]init];
    self.scrollView.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.94 alpha:1];
    //self.scrollView.backgroundColor = [UIColor redColor];
    self.scrollView.delegate = self;
    [self addSubview: self.scrollView];
    //显示滚动条
    self.scrollView.showsHorizontalScrollIndicator = NO;
   
    //设置小圆点
    self.pointView = [[UILabel alloc]init];
    self.pointView.backgroundColor = [UIColor colorWithRed:0.87 green:0.87 blue:0.87 alpha:1];
    self.pointView.layer.masksToBounds = YES;
    self.pointView.layer.cornerRadius = 5;
    
    
    [self.scrollView addSubview:self.pointView];
    self.pointView.tag = 3200;
    
   // self.titleArr = [@[@"最新", @"热门", @"亚洲", @"欧洲", @"美洲", @"非洲", @"大洋洲"]mutableCopy];
    
    //遍历创建按钮
    for (NSInteger i = 0; i < self.titleArr.count; i++) {
        self.btn = [UIButton buttonWithType:UIButtonTypeSystem];
        self.btn.tag = 3000 + i;

        [self.btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        //当没有点击时  默认第一个为点击状态
        if (i == 0) {
            [self.btn setTitleColor:[UIColor colorWithRed:0.71 green:0.78 blue:0.73 alpha:1]forState:UIControlStateNormal];
            self.btn.titleLabel.font = [UIFont systemFontOfSize:17];
        }
        //按钮触发事件
        [self.btn addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventTouchUpInside];
        [self.scrollView addSubview:self.btn];
    }
    
    
}
- (void)layoutSubviews
{
    [super layoutSubviews];

    
    //self.scrollView.frame = CGRectMake(0, 0, VWIDTH, 50);
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self).offset(0);
        make.left.mas_equalTo(self).offset(0);
        make.size.mas_equalTo(CGSizeMake(VWIDTH, VHEIGHT));
    }];
    [self.pointView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.scrollView.mas_top).offset(45);
        make.left.mas_equalTo(self.scrollView.mas_left).offset(25);
        make.size.mas_equalTo(CGSizeMake(40, 2));
    }];

    //设置偏移量
    
    self.scrollView.contentSize = CGSizeMake(VWIDTH / 5 * self.titleArr.count + 70, 0);
    //遍历给button frame值
    for (NSInteger i = 0; i < self.titleArr.count; i++) {
        ((UIButton *)[self viewWithTag:3000 + i]).frame = CGRectMake(i * VWIDTH / 5 + 10, 15, 70, 25);
        [((UIButton *)[self viewWithTag:3000 + i]) setTitle:self.titleArr[i] forState:UIControlStateNormal];
        
        
    }
    
}
- (void)changePage:(UIButton *)btn
{
    //让代理人实现协议方法 代理人为显示的controller
    [self.delegate ChangePage:btn];
    
    for (NSInteger i = 0; i < self.titleArr.count; i++) {
        
        if (btn.tag == 3000 + i) {
            [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionAllowAnimatedContent animations:^{
                self.pointView.frame = CGRectMake(i * VWIDTH / 5 + 10, 15, 70, 25);
            } completion:^(BOOL finished) {
                // NSLog(@"meatButton点击~");
            }];
        }
        
        
    }
    
 
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
